import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col, Table} from 'antd';
import {TABLE, DATE_FORMAT} from '../constants';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import routes from './routes';
import {injectIntl} from './intl';
import {lcKind, lcStatus, currency} from '../Enum';
import {conventEnumValueToString, formatDateTime, formatAmount} from '../utils';
import {getLccDetail} from './api';
const {Description} = DescriptionList;
export class LetterOfCredit extends React.PureComponent {
    state = {
        loading: false,
        data: {},
    }
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({loading: true});
        getLccDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        loading: false,
                        data: res.data,
                        details: res.data.details,
                        usedDetails: res.data.usedDetails
                    });
                else
                    this.setState(({
                        loading: false
                    }));
            });
    }
    render() {
        const {formatMessage} = this.props.intl;
        const {data} = this.state;
        const Yes = formatMessage({
            id: 'letterOfCredit.yes',
            defaultMessage: '是'
        });
        const No = formatMessage({
            id: 'letterOfCredit.no',
            defaultMessage: '否'
        });
        const columns = [{
            title: formatMessage({
                id: 'letterOfCredit.column.invoiceCode',
                defaultMessage: '发票号'
            }),
            dataIndex: 'invoiceCode',
        }, {
            title: formatMessage({
                id: 'letterOfCredit.column.cashInPrice',
                defaultMessage: '到款金额'
            }),
            dataIndex: 'cashInPrice',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'letterOfCredit.column.cashInDate',
                defaultMessage: '到款日期'
            }),
            dataIndex: 'cashInDate',
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'letterOfCredit.column.isCancel',
                defaultMessage: '是否已核销'
            }),
            dataIndex: 'isCancel',
            render: text => text === true ? formatMessage({
                id: 'letterOfCredit.column.yes',
                defaultMessage: '是'
            }) : formatMessage({
                id: 'letterOfCredit.column.no',
                defaultMessage: '否'
            })
        }, {
            title: formatMessage({
                id: 'letterOfCredit.column.registerName',
                defaultMessage: '登记人姓名'
            }),
            dataIndex: 'registerName',
        }, {
            title: formatMessage({
                id: 'letterOfCredit.column.registerTime',
                defaultMessage: '登记人时间'
            }),
            dataIndex: 'registerTime',
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'letterOfCredit.column.remark',
                defaultMessage: '备注'
            }),
            dataIndex: 'remark'
        }];
        const usedColumns = [{
            title: formatMessage({
                id: 'letterOfCredit.column.saleOrderCode',
                defaultMessage: '订单编号'
            }),
            dataIndex: 'saleOrderCode'
        }, {
            title: formatMessage({
                id: 'letterOfCredit.column.transBatchCode',
                defaultMessage: '发运批次号'
            }),
            dataIndex: 'transBatchCode'
        }, {
            title: formatMessage({
                id: 'letterOfCredit.column.transBatchPrice',
                defaultMessage: '批次总额'
            }),
            dataIndex: 'transBatchPrice',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'letterOfCredit.column.saleConfirmationCode',
                defaultMessage: '销售确认书编号'
            }),
            dataIndex: 'saleConfirmationCode'
        }, {
            title: formatMessage({
                id: 'letterOfCredit.column.orderPrice',
                defaultMessage: '订单金额'
            }),
            dataIndex: 'orderPrice',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'letterOfCredit.column.orderDate',
                defaultMessage: '订单日期'
            }),
            dataIndex: 'orderDate',
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'letterOfCredit.column.payMentDate',
                defaultMessage: '交单日期'
            }),
            dataIndex: 'payMentDate',
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'letterOfCredit.column.userName',
                defaultMessage: '使用人姓名'
            }),
            dataIndex: 'userName',
        }, {
            title: formatMessage({
                id: 'letterOfCredit.column.userTime',
                defaultMessage: '使用时间'
            }),
            dataIndex: 'userTime',
            render: text => formatDateTime(text, DATE_FORMAT)
        }];
        return (
            <div className="detail">
                <Card loading={this.state.loading}>
                    <DescriptionList size="large" title={<Row><Col span={6}>{formatMessage({
                        id: 'letterOfCredit.text.title',
                        defaultMessage: '信用证详情'
                    })}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.branchCode',
                            defaultMessage: '营销分公司编号'
                        })}>{data.branchCode}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.branchName',
                            defaultMessage: '营销分公司名称'
                        })}>{data.dealerName}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.brandCode',
                            defaultMessage: '品牌编号'
                        })}>{data.brandCode}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.brandName',
                            defaultMessage: '品牌名称'
                        })}>{data.brandName}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.lcCode',
                            defaultMessage: '信用证号码'
                        })}>{data.lcCode}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.versionCode',
                            defaultMessage: '版本号'
                        })}>{data.versionCode}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.dealerCode',
                            defaultMessage: '总代编号'
                        })}>{data.dealerCode}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.dealerName',
                            defaultMessage: '总代名称'
                        })}>{data.dealerName}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.lcApplicant',
                            defaultMessage: '开证申请人'
                        })}>{data.lcApplicant}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.issueCountry',
                            defaultMessage: '开证国家'
                        })}>{data.issueCountry}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.issueBank',
                            defaultMessage: '开证银行'
                        })}>{data.issueBank}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.issuePrice',
                            defaultMessage: '开证金额'
                        })}>{data.issuePrice}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.currency',
                            defaultMessage: '币种'
                        })}>{conventEnumValueToString(currency, data.currency)}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.lcPayMentDays',
                            defaultMessage: '信用证账期'
                        })}>{data.lcPayMentDays}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.issueDate',
                            defaultMessage: '开证日期'
                        })}>{formatDateTime(data.issueDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.lastLoadDate',
                            defaultMessage: '最迟装船日期'
                        })}>{formatDateTime(data.lastLoadDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.effectiveDate',
                            defaultMessage: '生效日期'
                        })}>{formatDateTime(data.effectiveDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.expirationDate',
                            defaultMessage: '失效日期'
                        })}>{formatDateTime(data.expirationDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.isAllowBatch',
                            defaultMessage: '是否允许分批'
                        })}>{data.isAllowBatch ? Yes : No}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.isAllowTransfer',
                            defaultMessage: '是否允许转运'
                        })}>{data.isAllowTransfer ? Yes : No}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.quantity',
                            defaultMessage: '件数'
                        })}>{data.quantity}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.lcKind',
                            defaultMessage: '信用证类型'
                        })}>{conventEnumValueToString(lcKind, data.lcKind)}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.lcStatus',
                            defaultMessage: '信用证状态'
                        })}>{conventEnumValueToString(lcStatus, data.lcStatus)}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.creatorName',
                            defaultMessage: '创建人姓名'
                        })}>{data.creatorName}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.createTime',
                            defaultMessage: '创建时间'
                        })}>{formatDateTime(data.createTime, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.modifierName',
                            defaultMessage: '修改人姓名 '
                        })}>{data.modifierName}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.modifyTime',
                            defaultMessage: '修改时间'
                        })}>{formatDateTime(data.modifyTime, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.abandonerName',
                            defaultMessage: '作废人姓名 '
                        })}>{data.abandonerName}</Description>
                        <Description term={formatMessage({
                            id: 'letterOfCredit.text.abandonTime',
                            defaultMessage: '作废时间'
                        })}>{formatDateTime(data.abandonTime, DATE_FORMAT)}</Description>
                    </DescriptionList>
                </Card>
                <Card>
                    <DescriptionList size="large" title={formatMessage({
                        id: 'letterOfCredit.table.details',
                        defaultMessage: '信用证到款清单'
                    })}>
                        <Table
                            className="white-space-nowrap"
                            columns={columns}
                            rowKey="id"
                            pagination={false}
                            dataSource={this.state.details}
                            {...TABLE} />
                    </DescriptionList>
                </Card>
                <Card>
                    <DescriptionList size="large" title={formatMessage({
                        id: 'letterOfCredit.table.usedDetails',
                        defaultMessage: '信用证使用清单'
                    })}>
                        <Table
                            className="white-space-nowrap"
                            columns={usedColumns}
                            rowKey="id"
                            pagination={false}
                            dataSource={this.state.usedDetails}
                            {...TABLE} />
                    </DescriptionList>
                </Card>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.state.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'letterOfCredit.text.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

LetterOfCredit.propTypes = {
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
};

import {connect} from 'react-redux';
const mapStateToProps = (state, props) => ({
    id: props.letterOfCreditId,
});
const mapDispatchToProps = (dispatch, props) => ({
    onCancel: () => {
        props.history.push(routes.detail.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(LetterOfCredit));
